/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package GuiTools;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;

import GuiTools.*;
import Settings.*;

/**
 * Provides a graphical interface of buttons to insert ISP notation.
 * @author Christoph Schnack
 *
 */
public class EditPhoneticDialog extends JDialog{
	JLabel display;
	TribuitTextField displayTF;
	JDialog phonFrame;
	TribuitFrame parentFrame;
	Settings SET;
	Language LANG;
	String initialValue;
	boolean aborted = true;
	GridBagConstraints gbc = new GridBagConstraints();
	Font buttonFont = new Font(Font.SANS_SERIF,Font.BOLD,22), titleFont = new Font(Font.SERIF,Font.BOLD,27);
	
	public EditPhoneticDialog(TribuitFrame parentFrm, Settings set, String initValue){
		super(parentFrm,set.getLanguage().getEdit(),true);
		phonFrame = this;
		initialValue = initValue;
		parentFrame = parentFrm;
		SET = set;
		LANG = set.getLanguage();
		gbc.insets = new Insets(5,5,5,5);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.ipadx = 10;
		gbc.ipady = 10;
		buildGUI();
	}
	
	public void buildGUI(){
		JPanel keysPanel = new JPanel();
		keysPanel.setLayout(new BoxLayout(keysPanel,BoxLayout.Y_AXIS));
		
		JLabel alphLabel = new JLabel(LANG.getAlphabet());
		alphLabel.setFont(titleFont);
		keysPanel.add(alphLabel);
		int start = Integer.parseInt("0021",16);
		int end = Integer.parseInt("007B",16);
		JPanel alphPanel = new JPanel(new GridLayout(((end-start)/15),15));
		for(int i=start;i<end;i++){
			if(GuiTools.checkInput(""+(char)i))alphPanel.add(new CharButton(""+(char) i));
		}
		//brackets
		alphPanel.add(new CharButton(""+(char) Integer.parseInt("0028",16)));
		alphPanel.add(new CharButton(""+(char) Integer.parseInt("0029",16)));
		keysPanel.add(alphPanel);
		
		JLabel letterLabel = new JLabel(LANG.getCharacters());
		letterLabel.setFont(titleFont);
		keysPanel.add(letterLabel);
		start = Integer.parseInt("0250",16);
		end = Integer.parseInt("0300",16);
		JPanel keyPanel = new JPanel(new GridLayout(((end-start)/15),15));
		keyPanel.add(new CharButton(""+(char) Integer.parseInt("00E6",16)));
		for(int i=start;i<end;i++){
			if(GuiTools.checkInput(""+(char)i))keyPanel.add(new CharButton(""+(char) i));
		}
		keysPanel.add(keyPanel);
		
		JLabel modLabel = new JLabel(LANG.getModifier());
		modLabel.setFont(titleFont);
		keysPanel.add(modLabel);
		start = Integer.parseInt("0300",16);
		end = Integer.parseInt("0350",16);
		JPanel modPanel = new JPanel(new GridLayout(((end-start)/15),15));
		for(int i=start;i<end;i++){
			if(GuiTools.checkInput(""+(char)i))modPanel.add(new CharButton(" "+(char) i));
		}
		keysPanel.add(modPanel);
		
		
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(northPanel,BoxLayout.Y_AXIS));
		JLabel wordLabel = new JLabel();
		wordLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		wordLabel.setFont(new Font(Font.SERIF,Font.BOLD,30));
		String dis = (initialValue.equals("~")) ? "" : initialValue;
		displayTF = new TribuitTextField(SET,(initialValue.equals("~")) ? "" : initialValue,10);
		display = new JLabel(dis);
		display.setFont(buttonFont);
		JButton delete = new JButton("\u00AB");
		delete.setToolTipText(LANG.getDelete());
		delete.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s = display.getText();
				if(!s.equals("")){
					display.setText(s.substring(0,s.length()-1));
					displayTF.setText(s.substring(0,s.length()-1));
				}
			}
		});
		JPanel displayPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		displayPanel.add(display);
		displayPanel.add(displayTF);
		displayPanel.add(delete);
		northPanel.add(wordLabel);
		northPanel.add(displayPanel);
		JButton pOkBtn = new JButton(LANG.getAccept());
		pOkBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(display.getText().equals("")) initialValue = "~";
				else initialValue = display.getText();
				aborted = false;
				phonFrame.dispose();
				phonFrame=null;
			}
		});
		JButton pCancelBtn = new JButton(LANG.getCancel());
		pCancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				initialValue = null;
				phonFrame.dispose();
				phonFrame=null;
			}
		});
		JPanel btnPanel = new JPanel();
		btnPanel.add(pOkBtn);
		btnPanel.add(pCancelBtn);
		JScrollPane jsp = new JScrollPane(keysPanel);
		jsp.setPreferredSize(new Dimension(850,400));
		jsp.getVerticalScrollBar().setUnitIncrement(20);
		phonFrame.getContentPane().add(BorderLayout.NORTH,northPanel);
		phonFrame.getContentPane().add(BorderLayout.CENTER,jsp);
		phonFrame.getContentPane().add(BorderLayout.SOUTH,btnPanel);
		phonFrame.setLocation(100,0);
		phonFrame.pack();
		phonFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				initialValue = null;
			}
		});
		phonFrame.setVisible(true);
	}
	
	public String getResult(){
		return initialValue;
	}
	
	
	
	private class CharButton extends JPanel{
		String text;
		JLabel label = new JLabel();
		public CharButton(String txt){
			super();
			text = txt;
			label.setHorizontalAlignment(label.CENTER);
			label.setText(text);
			label.setFont(buttonFont);
			label.setBackground(Color.lightGray);
			label.setOpaque(true);
			this.setLayout(new GridBagLayout());
			this.add(label,gbc);
			this.setBackground(Color.DARK_GRAY);
			this.addMouseListener(new ButtonListener());
		}
		
		private class ButtonListener implements MouseListener{

			public void mouseClicked(MouseEvent arg0) {
			}
			public void mouseEntered(MouseEvent arg0) {
				label.setForeground(Color.red);
				label.repaint();
			}

			public void mouseExited(MouseEvent arg0) {
				label.setForeground(Color.black);
				label.repaint();
			}
			
			public void mousePressed(MouseEvent arg0) {
				displayTF.setText(display.getText()+text.trim());
				display.setText(display.getText()+text.trim());
			}
			public void mouseReleased(MouseEvent arg0) {}
			
		}
		
		public String getButtonText(){ return text;}
	}
	
	public boolean getAborted(){
		return aborted;
	}
}
